package com.xms.security.service.dataauth;

import com.xms.core.enums.AccessRightValue;
import com.xms.core.exception.XmsException;
import com.xms.datacore.service.CreaterService;
import com.xms.schema.entity.Entity;
import com.xms.security.entity.EntityPermission;
import com.xms.utils.UUIDUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * EntityPermissionCreaterService
 * </p>
 *
 * @author migo
 * @since 2021/10/8
 */
@CacheConfig(cacheNames = "entitypermission", keyGenerator = "CacheKeyGenerator")
@Service
public class EntityPermissionCreaterService extends CreaterService<EntityPermission> implements IEntityPermissionCreaterService {

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean create(EntityPermission entity) throws SQLException, XmsException {
        return super.save(entity);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean createMany(Collection<EntityPermission> entityList) throws SQLException, XmsException {
        for (EntityPermission entity : entityList){
            super.save(entity);
        }
        return true;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean createDefaultPermissions(Entity entity) throws XmsException, SQLException {
        return this.createMany(getDefaultPermissions(entity));
    }

    private List<EntityPermission> getDefaultPermissions(Entity entity)
    {
        List<EntityPermission> entityPermissions = new ArrayList<>();
        entityPermissions.add(new EntityPermission() {{setEntityid(entity.getEntityid());setEntitypermissionid(UUIDUtil.get());setAccessright(AccessRightValue.Read.value());setName(entity.getName() + ".read");setState(true);} });
        entityPermissions.add(new EntityPermission() {{setEntityid(entity.getEntityid());setEntitypermissionid(UUIDUtil.get());setAccessright(AccessRightValue.Create.value());setName(entity.getName() + ".create");setState(true);} });
        entityPermissions.add(new EntityPermission() {{setEntityid(entity.getEntityid());setEntitypermissionid(UUIDUtil.get());setAccessright(AccessRightValue.Update.value());setName(entity.getName() + ".update");setState(true);} });
        entityPermissions.add(new EntityPermission() {{setEntityid(entity.getEntityid());setEntitypermissionid(UUIDUtil.get());setAccessright(AccessRightValue.Delete.value());setName(entity.getName() + ".delete");setState(true);} });
        entityPermissions.add(new EntityPermission() {{setEntityid(entity.getEntityid());setEntitypermissionid(UUIDUtil.get());setAccessright(AccessRightValue.Export.value());setName(entity.getName() + ".export");setState(true);} });
        entityPermissions.add(new EntityPermission() {{setEntityid(entity.getEntityid());setEntitypermissionid(UUIDUtil.get());setAccessright(AccessRightValue.Import.value());setName(entity.getName() + ".import");setState(true);} });
        entityPermissions.add(new EntityPermission() {{setEntityid(entity.getEntityid());setEntitypermissionid(UUIDUtil.get());setAccessright(AccessRightValue.Share.value());setName(entity.getName() + ".share");setState(true);} });
        entityPermissions.add(new EntityPermission() {{setEntityid(entity.getEntityid());setEntitypermissionid(UUIDUtil.get());setAccessright(AccessRightValue.Assign.value());setName(entity.getName() + ".assign");setState(true);} });
        entityPermissions.add(new EntityPermission() {{setEntityid(entity.getEntityid());setEntitypermissionid(UUIDUtil.get());setAccessright(AccessRightValue.Append.value());setName(entity.getName() + ".append");setState(true);} });
        entityPermissions.add(new EntityPermission() {{setEntityid(entity.getEntityid());setEntitypermissionid(UUIDUtil.get());setAccessright(AccessRightValue.AppendTo.value());setName(entity.getName() + ".appendto");setState(true);} });
        return entityPermissions;
    }
}
